<?php

namespace app\api\controller\User;

use think\Console;
use think\Controller;
use think\Request;

class Address extends Controller
{
    public function __construct()
    {
        parent::__construct();

        $this->UserModel = model('User.User');
        $this->AddressModel = model('User.Address');
        $this->RegionModel = model('Region');
    }

    public function index()
    {
        if ($this->request->isPost()) {
            $userid = $this->request->param('userid', 0);

            $data = $this->AddressModel->with(['provinces', 'citys', 'districts'])->where(['userid' => $userid])->order('status', 'desc')->select();

            // halt($data);

            $this->success('返回收货地址数据', null, $data);
        }
    }

    public function add()
    {
        if ($this->request->isPost()) {
            $userid = $this->request->param('userid', 0);

            $code = $this->request->param('code', 0);

            $params = $this->request->param();

            $user = $this->UserModel->find($userid);

            if (!$user) {
                $this->error('用户不存在');
                exit;
            }

            $area = $this->RegionModel->where(['code' => $code])->value('parentpath');

            if (!$area) {
                $this->error('你所选的地区不存在');
                exit;
            }

            $AreaList = explode(',', $area);

            // 把数组里的值赋值给新的变量
            list($province, $city, $district) = $AreaList;

            $data = [
                'consignee' => $params['name'],
                'mobile' => $params['tel'],
                'address' => $params['address'],
                'userid' => $userid,
                'province' => $province,
                'city' => $city,
                'district' => $district,
                'status' => $params['status']
            ];

            if ($data['status'] == 1) {
                // 把这个用户的所有的地址设置为非默认
                $Updata = $this->AddressModel->where('userid', $userid)->update(['status' => 0]);

                if ($Updata === FALSE) {
                    $this->error('更新默认地址失败');
                    exit;
                }
            }

            $result = $this->AddressModel->save($data);


            if ($result === FALSE) {
                $this->error('新增收货地址失败');
                exit;
            } else {
                $this->success('新增收货地址成功', '/user/address/index', $data);
                exit;
            }
        }
    }

    public function select()
    {
        if ($this->request->isPost()) {
            $userid = $this->request->param('userid', 0);

            $addrid = $this->request->param('addrid', 0);

            $user = $this->UserModel->find($userid);

            if (!$user) {
                $this->error('用户不存在');
                exit;
            }

            $address = $this->AddressModel->find($addrid);

            if (!$address) {
                $this->error('你所选的收货地址不存在');
                exit;
            }

            if ($address['userid'] != $userid) {
                $this->error('数据异常');
                exit;
            }

            $Updata = $this->AddressModel->where(['userid' => $userid])->update(['status' => 0]);

            if ($Updata === FALSE) {
                $this->error('更新收货地址失败');
                exit;
            }

            $result = $this->AddressModel->where(['id' => $addrid])->update(['status' => 1]);

            if ($result === FALSE) {
                $this->error('更新默认收货地址失败');
                exit;
            } else {
                $this->success('更新默认地址成功');
                exit;
            }
        }
    }


    public function info()
    {
        if ($this->request->isPost()) {
            $userid = $this->request->param('userid', 0);

            $addrid = $this->request->param('addrid', 0);
            

            $user = $this->UserModel->find($userid);

            $address = $this->AddressModel->with(['provinces', 'citys', 'districts'])->find($addrid);

            if (!$user) {
                $this->error('用户不存在');
                exit;
            }

            if ($address['userid'] != $userid) {
                $this->error('你不能编辑他人的收货地址');
                exit;
            }

            if (!$address) {
                $this->error('所选的收货地址不存在');
                exit;
            }

            $this->success('返回收货地址数据', null, $address);
        }
    }

    public function delete()
    {
        if ($this->request->isPost()) {
            $userid = $this->request->param('userid', 0);

            $addrid = $this->request->param('addrid', 0);

            $user = $this->UserModel->find($userid);

            $address = $this->AddressModel->with(['provinces', 'citys', 'districts'])->find($addrid);

            if (!$user) {
                $this->error('用户不存在');
                exit;
            }

            if ($address['userid'] != $userid) {
                $this->error('你不能删除他人的收货地址');
                exit;
            }

            if (!$address) {
                $this->error('所选的收货地址不存在');
                exit;
            }

            $result = $this->AddressModel->where(['id' => $addrid])->delete();

            if ($result === FALSE) {
                $this->error('删除失败');
                exit;
            } else {
                $this->success('删除成功');
                exit;
            }
        }
    }

    public function update()
    {
        if ($this->request->isPost()) 
        {
            $userid = $this->request->param('userid', 0);

            $addrid = $this->request->param('addrid', 0);

            $code = $this->request->param('code', 0);

            $params = $this->request->param();

            $address = $this->AddressModel->with(['provinces', 'citys', 'districts'])->find($addrid);

            if (!$address) {
                $this->error('所选的收货地址不存在');
                exit;
            }

            $area = $this->RegionModel->where(['code' => $code])->value('parentpath');

            if (!$area) {
                $this->error('你所选的地区不存在');
                exit;
            }

            $AreaList = explode(',', $area);

            // 把数组里的值赋值给新的变量
            list($province, $city, $district) = $AreaList;
            $data=[
                'mobile' => $params['tel'],
                'consignee' => $params['name'],
                'address' => $params['address'],
                'province' => $province,
                'city' => $city,
                'district' => $district,
                'status' => $params['status']
            ];
            if ($data['status'] == 1) {
                // 把这个用户的所有的地址设置为非默认
                $Updata = $this->AddressModel->where('userid', $userid)->update(['status' => 0]);

                if ($Updata === FALSE) {
                    $this->error('更新默认地址失败');
                    exit;
                }
            }

            $result = $this->AddressModel->where(['id' => $addrid])->update($data);

            if ($result === FALSE) {
                $this->error('保存失败');
                exit;
            } else {
                $this->success('保存成功');
                exit;
            }
        }
       
    }
    public function order()
    {
        if ($this->request->isPost()) {
            $userid = $this->request->param('userid', 0);

            $address = $this->AddressModel->where(['userid' => $userid, 'status' => 1])->find();

            if ($address === FALSE) {
                $this->error('获取默认收货地址失败');
                exit;
            } else {
                $this->success('返回默认收货地址', null, $address);
                exit;
            }
        }
    }
    public function edit()
    {
        if($this->request->isPost())
        {
            $userid = $this->request->param('userid',0);
            $addrid = $this->request->param('addrid',0);
            $code = $this->request->param('code',0);
            $AddeData = $this->request->param();
            $user = $this->UserModel->find($userid);

            if(!$user)
            {
                $this->error('用户不存在');
                exit;
            }

            $address = $this->AddressModel->find($addrid);

            if(!$address)
            {
                $this->error('该收货地址不存在');
                exit;
            }

            if($address['userid'] != $userid)
            {
                $this->error('你不能修改他人的收货地址');
                exit;
            }

            $parentpath = $this->RegionModel->where(['code' => $code])->value('parentpath');

            if(!$parentpath)
            {
                $this->error('所选地区不存在');
                exit;
            }

            $data = [
                'id' => $addrid,
                'consignee' => $AddeData['consignee'],
                'mobile' => $AddeData['mobile'],
                'address' => $AddeData['address'],
                'userid' => $AddeData['userid'],
                'status' => $AddeData['status'],
            ];


            [$province,$city,$district] = explode(',',$parentpath);

            if($province)
            {
                $data['province'] = $province;
            }

            if($city)
            {
                $data['city'] = $city;
            }

            if($district)
            {
                $data['district'] = $district;
            }

            $result = $this->AddressModel->validate('common/User/Address')->isUpdate(true)->save($data);

            if($result === FALSE)
            {
                $this->error($this->AddressModel->getError());
                exit;
            }else{
                $this->success('更新成功');
                exit;
            }

        }
    }
}
